package tur.br.moradadosol.view.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import tur.br.moradadosol.model.Usuario;

public class LoginForm extends ActionForm {
	
	private Usuario usuario;
	private String btnSubmit;
	
	public LoginForm() {
		usuario = new Usuario();
	}
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setBtnSubmit("");
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		
		ActionErrors errors = new ActionErrors();
		
		if(getBtnSubmit().length() > 0) {
			
			if(usuario.getUsuario() == null ||
					usuario.getUsuario().trim().length() == 0) {
				errors.add("login.error", 
					new ActionMessage("error.login.identificador.required"));
			}
			
			if(usuario.getSenha() == null ||
					usuario.getSenha().trim().length() == 0) {
				errors.add("login.error", 
					new ActionMessage("error.login.senha.required"));
			}
		}
		
		return errors;
	}
	
	public void setBtnSubmit(String value) {
		this.btnSubmit = value;
	}
	
	public String getBtnSubmit() {
		return btnSubmit;
	}
	
	public void setUsuario(String nome) {
		usuario.setUsuario(nome);
	}
	
	public String getUsuario() {
		return usuario.getUsuario();
	}
	
	public void setSenha(String senha) {
		usuario.setSenha(senha);
	}
	
	public String getSenha() {
		return usuario.getSenha();
	}
	
	public Usuario getObjUsuario() {
		return usuario;
	}
}
